package com.boyon.system.action;

import java.util.Arrays;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boyon.common.domain.UserSession;
import com.boyon.common.status.Constant;
import com.boyon.system.entity.Account;
import com.boyon.system.entity.Role;
import com.boyon.system.service.AccountService;
import com.boyon.system.service.RoleService;
import com.boyon.web.param.annotation.ValidatorRequestBody;
import com.boyon.web.response.AbstractResponse;
import com.boyon.web.response.FailResponse;
import com.boyon.web.response.SuccessResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * ClassName:IndexAction <br/>
 * Function: index相关. <br/>
 * Date: 2018年3月15日 下午5:48:48 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Controller
@RequestMapping(value = "/beyon", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
public class IndexAction {

	@Autowired
	private AccountService accountService;
	@Autowired
	private RoleService roleService;

	/**
	 * 
	 * login:登录系统. <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public SuccessResponse login(@ValidatorRequestBody Account account,
			HttpSession session) {
		String sessionId = session.getId();
		log.info("boyon login sessionId={}", sessionId);
		Long accountId = accountService.login(account, sessionId);
		Account entity = accountService.findByAccountId(accountId);
		UserSession userSession = accountService.createUserSession(entity);
		userSession.setUserSessionId(sessionId);
		session.setAttribute(Constant.SESSION_USER, userSession);
		Role role = roleService.findByRoleId(entity.getRoleId());
		session.setAttribute(Constant.SESSION_USER_AUTHORITY,
				Arrays.asList(role.getAuthority().split(",")));
		return new SuccessResponse(userSession);
	}

	/**
	 * 
	 * home:首次访问系统. <br/>
	 * 
	 * @author sunshulin
	 * @param session
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/home")
	@ResponseBody
	public AbstractResponse home(HttpSession session) {
		String sessionId = session.getId();
		log.info("boyon home sessionId={}", sessionId);
		UserSession userSession = (UserSession) session
				.getAttribute(Constant.SESSION_USER);
		if (userSession != null) {
			return new SuccessResponse(userSession);
		} else {
			return new FailResponse(204, sessionId);
		}
	}

	/**
	 * 
	 * logout:退出登录. <br/>
	 * 
	 * @author sunshulin
	 * @param session
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/logout")
	@ResponseBody
	public AbstractResponse logout(HttpSession session) {
		String sessionId = session.getId();
		log.info("boyon logout sessionId={}", sessionId);
		session.removeAttribute(Constant.SESSION_USER);
		return new SuccessResponse();
	}
}
